/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author pro
 * @version 1.0
 * @date 2016-03-07
 */
package com.eyaomall.wxmall.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.service.ShopService;
import com.lycheeframework.core.common.helper.GlobEnv;

/**
 * shop拦截器
 * 
 * @author pro
 *
 */
public class ShopInterceptor extends HandlerInterceptorAdapter {

	private final static Logger LOG = LoggerFactory.getLogger(ShopInterceptor.class);

	@Autowired
	private ShopService shopService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		Integer shopid = 0;
		LOG.error("shopInterceptoer:" + request.getServletPath());
		if (request.getServletPath().equals("/") || request.getServletPath().equalsIgnoreCase("/member/index")) {
			try {
				shopid = Integer.parseInt((request.getParameter("shopId")));
			} catch (Exception e) {
				LOG.error(e.getMessage());
			}
			LOG.error("shopInterceptoer_id" + shopid);
		}
		if (shopService.queryShopInvalid(shopid > 0 ? shopid : MemberHelper.getShopId()) == null) {
			response.sendRedirect(GlobEnv.getWebURL("error/nomoney"));
			return false;
		}

		return true;
	}

}
